<?php
namespace Admin\Controller;

use Common\Util\UploadHandler;
use Common\Util\BankCard;
use Common\Util\Page;
use Common\Controller\AdminbaseController;

class CardController extends AdminbaseController {
	
	protected $userModel;
	protected $beauticianModel;
	protected $beautyParlorModel;
	
    public function getCardInfo() {
    	$card = I('get.card');
    	
    	$BankCard = new BankCard ();
    	$cardInfo = $BankCard->query ( $card );
    	$this->ajaxReturn(array('status' => 1, 'data' => $cardInfo));
    }
    
    public function add() {
    	if (IS_POST) {
    		$cardModel = D ( '/Home/Model/Card' );
    		 
    		$target_id = I('post.target_id');
    		$card = I('post.card');
    		$cardHolder = I('post.cardHolder');
    		$where = array('target_id' => $target_id, 'card' => $card);
    		
    		$cardBindRecord = $cardModel->where($where)->find();
    		if (!$cardBindRecord) {
    			$BankCard = new BankCard ();
    			$cardInfo = $BankCard->query ( $card );
    			
    			if (!$cardInfo) {
    				$this->ajaxReturn(array('status' => 0, 'message' => '获取银行卡信息失败'));
    			}
    			 
    			$last_four_number = substr ( $card, -4 );
    			$bank_name = $cardInfo ['bank_name'];
    			$card_type = $cardInfo ['card_type'];
    			$data = array (
    					'cardHolder' => $cardHolder,
    					'card' => $card,
    					'bank_name' => $bank_name,
    					'card_type' => $card_type,
    					'target_id' => $target_id,
    					'last_four_number' => $last_four_number,
    					'status'	=> 1,
    					'created' => time()
    			);
    			if ($cardModel->add ( $data )) {
    				$this->ajaxReturn(array('status' => 1, 'url' => U ( '/Admin/Card/index/id/'.$id )));
    			} else {
    				$this->ajaxReturn(array('status' => 0, 'message' => '获取银行卡信息失败'));
    			}
    		} else {
    			if ($cardBindRecord ['status'] == 1) {
    				$this->ajaxReturn(array('status' => 0, 'message' => '改银行卡已绑定，请选择其它银行卡'));
    			} else {
    				$result = $cardModel->where($where)->save(array('status' => 1));
    				if ($result) {
    					$this->ajaxReturn(array('status' => 1, 'url' => U ( '/Admin/Card/index/id/'.$id )));
    				} else {
    					$this->ajaxReturn(array('status' => 0, 'message' => '获取银行卡信息失败'));
    				}
    			}
    		}
    	} else {
    		$id = I('get.id');
    		
    		$type = D('AutoId')->field('type')->where(array('id' => $id))->find();
    	
    		$targetInfo = array();
    		if ($type ['type'] == 'beauty_parlor') {
    			$targetInfo = D('BeautyParlor')->where(array('id' => $id))->find();
    		} elseif ($type ['type'] == 'beautician') {
    			$targetInfo = D('Beautician')->where(array('id' => $id))->find();
    		}
    		
    		$this->assign('targetInfo', $targetInfo);
    		$this->assign('id', $id);
    		$this->display();
    	}
    }
    
    public function index() {
    	$userId = I('get.id');
    	
    	$card = I('get.card');
    	$cardHolder = I('get.cardHolder');
    	$where = array();
    	if ($userId) {
    		$where ['target_id'] = $userId;
    	}
    	
    	if ($card) {
    		$where ['card'] = array('like', '%'.$card.'%');
    	}
    	
    	if ($cardHolder) {
    		$where ['cardHolder'] = array('like', '%'.$cardHolder.'%');
    	}
    	
    	$CardModel = D('Home/Model/Card');
    	$count = $CardModel->where($where)->count();
    	
    	$page = $this->page($count, 20);
    	
    	$field = array('c.*');
    	if ($userId) {
    		$type = D('AutoId')->field('type')->where(array('id' => $userId))->find();
    		
    		if ($type ['type'] == 'user') {
    			$field ['u.username'] = 'name';
    			$CardModel->join('LEFT JOIN '.C('DB_PREFIX').'user as u on c.target_id=u.id');
    		} elseif ($type ['type'] == 'beauty_parlor') {
    			$field [] = 'bp.name';
    			$CardModel->join('LEFT JOIN '.C('DB_PREFIX').'beauty_parlor as bp on c.target_id=bp.id');
    		} elseif ($type ['type'] == 'beautician') {
    			$field [] = 'b.name';
    			$CardModel->join('LEFT JOIN '.C('DB_PREFIX').'beautician as b on c.target_id=b.id');
    		}
    	} else {
    		$field [] = 'ai.type';
    		$CardModel->join('LEFT JOIN '.C('DB_PREFIX').'auto_id as ai on c.target_id=ai.id');
    	}
    		
    	$list = $CardModel->alias('c')->where($where)
    		->field($field)
    		->limit($page->firstRow.','.$page->listRows)
    	->select();
    	
    	if ($list) {
    		$targetInfo = array();
    		foreach ($list as $key => &$value) {
    			if (isset($targetInfo [$value ['target_id']])) {
    				$value ['name'] = $targetInfo [$value ['target_id']];
    			} else {
    				if ($value ['type'] == 'user') {
    					if (!isset($this->userModel)) {
    						$this->userModel = D('User');
    					}
    					$info = $this->userModel->getUserById($value ['target_id']);
    					$name = $info ['username'];
    				} else if ($value ['type'] == 'beauty_parlor') {
    					if (!isset($this->beautyParlorModel)) {
    						$this->beautyParlorModel = D('BeautyParlor');
    					}
    					$info = $this->beautyParlorModel->getBeautyInfo($value ['target_id']);
    					$name = $info ['name'];
    				} else {
    					if (!isset($this->beauticianModel)) {
    						$this->beauticianModel = D('Beautician');
    					}
    					$info = $this->beauticianModel->getBeauticianInfo($value ['target_id']);
    					$name = $info ['name'];
    				}
    				
    				$value ['name'] = $name;
    				$targetInfo [$value ['target_id']] = $name;
    			}
    		}
    	}
    	
    	
    	$this->assign('user_id', $userId);
    	$this->assign('list', $list);
    	
    	$this->assign('Page', $page->show('Admin'));
    	$this->display();
    }
    
    public function delete() {
    	$CardModel = D('Home/Model/Card');
    	 
    	if (!IS_POST) {
    		$id = I ('get.id');
    		 
    		$result = $CardModel->where(array('id' => $id))->save(array('status' => 0));
    		if ($result) {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    			 
    			$this->ajaxReturn(array('status' => 1, 'url' => U('index')));
    		} else {
    			$this->ajaxReturn(array('status' => 0));
    		}
    	} else {
    		$ids = I ('post.ids');
    		if (count($ids) == 1) {
    			$where = array('id' => $ids[0]);
    			$result = $CardModel->where($where)->save(array('status' => 0));
    		} else {
    			$where ['id'] = array('in', implode(',',$ids));
    			$result = $CardModel->where($where)->save(array('status' => 0));
    		}
    		 
    		if (!$result) {
    			$this->ajaxReturn(array('status' => 0 ));
    		}
    		 
    		foreach ($ids as $cardId) {
    			$operation = array('operation_object_id' => $cardId);
    			\Think\Hook::listen('operation', $operation);
    		}
    		 
    		$this->ajaxReturn(array('status' => 1, 'url' => U('index')));
    	}
    }
}